package com.lvfeng.system.controller;

import java.io.FileOutputStream;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.lvfeng.tool.pic.PicTool;

/**
 * @author Administrator
 * 文件上传控制器
 *
 */
@Controller
@RequestMapping("/fileup")	//接管所有的fileup路径请求
public class FileUpController {
	//001.单击跳转到上传文件页面
	@GetMapping("/ai/ocr")
	public String fileup() {
        return "fileup/ai/ocr";
    }
	@GetMapping("/ai/image")
	public String image() {
		return "fileup/ai/image";
	}
	@GetMapping("/ai/sound")
	public String sound() {
		return "fileup/ai/sound";
	}

	//002.上传图片，consumes代表输入
	@PostMapping(value = "/photos",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	public String addPic(@RequestParam("photo") MultipartFile picfile) throws Exception{
		FileOutputStream fos = new FileOutputStream("target/" + picfile.getOriginalFilename());
		IOUtils.copy(picfile.getInputStream(), fos);	//apache的commonIO，这里是把文件输入流整到fos里
		fos.close();
        return "fileup/success";
    }
	
	//003.下载图片
	@GetMapping(value = "/headpic",produces = MediaType.IMAGE_JPEG_VALUE)
	public byte[] getHeadPic() throws Exception{
		String imgPath = "src/test/icon.jpg";
//		InputStream ins = new FileInputStream(imgPath);
		return PicTool.readImagePathToByte(imgPath);
	}
	
	
	
}
